package com.scut.fy.common.web.model;

import org.springframework.http.HttpStatus;

import java.beans.Transient;
import java.io.Serializable;

/**
 * TODO
 *
 * @author JY
 * @version 1.0
 * @date 2021/8/13 17:57
 */
public class HttpResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    public static final int OK = HttpStatus.OK.value();

    public static final int BAD_REQUEST = HttpStatus.BAD_REQUEST.value();

    public static final int UNAUTHORIZED = HttpStatus.UNAUTHORIZED.value();

    private String msg = "OK";

    private int code = OK;

    private T data;

    public HttpResult() {
        super();
    }


    public HttpResult(int code, String msg, T data) {
        super();
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public HttpResult(final T data) {
        super();
        if (data instanceof Boolean) {
            if (!(Boolean) data) {
                this.msg = "error";
                this.code = 1;
            }
        }
        this.data = data;
    }

    public HttpResult(final T data, final String msg) {
        super();
        if (data instanceof Boolean) {
            if (!(Boolean) data) {
                this.msg = msg;
                this.code = OK;
            }
        }
        if (msg!=null) {
            this.msg = msg;
        }
        this.data = data;
    }

    public HttpResult(final Throwable e) {
        super();
        this.msg = e.getMessage();
        this.code = BAD_REQUEST;
    }

    @Transient
    public boolean isSuccess(){
        return getCode()==OK;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(final int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(final T data) {
        this.data = data;
    }
}
